package com.qire.manhua.dal.service;

import com.qire.antsbinder.dal.DataService;
import com.qire.antsbinder.dal.annotation.StrategyAnnotation;
import com.qire.common.basic.ObjectUtil;
import com.qire.common.basic.StringUtil;
import com.qire.common.constant.Url;
import com.qire.common.dal.WarehouseFactory;
import com.qire.common.model.entity.UserEntity;
import com.qire.manhua.dal.IPageDataDAL;
import com.qire.manhua.model.entity.HomeModuleEntity;
import com.qire.manhua.model.entity.HomeModuleEntity.HomeModuleRankEntity;
import com.qire.manhua.model.enumeration.HomeModuleType;
import com.qire.manhua.model.viewModel.impl.RecommendHotViewModel;
import com.qire.manhua.model.vo.HomeRecommendModuleVO;
import com.qire.manhua.model.vo.HomeRecommendModuleVO.ModuleInfoVO;
import com.qire.manhua.model.vo.HomeRecommendModuleVO.ModuleItemVO;
import com.qire.manhua.model.vo.HomeRecommendModuleVO.ModuleAdVO;
import com.qire.manhua.model.vo.HomeRecommendModuleVO.RankListVO;
import com.qire.manhua.model.vo.HomeRecommendModuleVO.RankListVO.RankInfoVO;
import com.qire.manhua.model.vo.HomeRecommendSlideVO;
import com.qire.manhua.model.vo.HomeRecommendVO;
import com.qire.manhua.model.vo.HomeRecommendVO.GuessItemVO;

public class RecommendHotService extends DataService {

    private IPageDataDAL pageData = WarehouseFactory.create(this, IPageDataDAL.class);

    public RecommendHotViewModel viewModel;

    public RecommendHotService(RecommendHotViewModel viewModel) {
        this.viewModel = viewModel;
    }

    // 更新数据
    public void loadPageData() {
        pageData.fetchRecommendHotPageData(UserEntity.self.getEnablePreferences());
    }

    @StrategyAnnotation(event = Url.HomeRecommendHotData)
    public void fetchRecommendHotPageDataCallBack(HomeRecommendVO data, boolean isCache) {
        viewModel.refreshLayoutFinish(300);

        if (ObjectUtil.isNull(data)) {
            return;
        }

        if(ObjectUtil.nonNull(data.slide)) {
            viewModel.clearBannerList();
            for(HomeRecommendSlideVO vo : data.slide) {
                viewModel.addBannerItemViewModel(vo.toAppAdvert());
            }
            viewModel.refreshBannerAdapter();
        }

        if(ObjectUtil.nonNull(data.modules)) {
            viewModel.clearModuleList();
            for(HomeRecommendModuleVO moduleVO : data.modules) {
                ModuleInfoVO infoVO = moduleVO.module;
                HomeModuleEntity moduleEntity   = new HomeModuleEntity(infoVO.id, infoVO.name, infoVO.direction);

                if(ObjectUtil.nonNull(moduleVO.list)) {
                    for(ModuleItemVO itemVO : moduleVO.list) {
                        int bookId              = itemVO.book_id;
                        String bookName         = itemVO.book_name;
                        String title            = itemVO.title;
                        String coverImg         = itemVO.cover_img;
                        String[] tags           = itemVO.tags;
                        String floatTips        = ""; // 空值传递将不会展示
                        // 以下属性在这个接口都未有数据不需要暂时给默认值
                        String shortTitle       = "";
                        int freeNum             = 0;
                        int lastReadChapterId   = 0;
                        String lastReadChapterName = "";
                        boolean  isTeensVisible = itemVO.isTeensVisible();

                        if(itemVO.bookInfo != null && itemVO.lastReadChapter != null && itemVO.lastUpdateChapter != null) {
                            bookId           = itemVO.bookInfo.id;
                            bookName         = itemVO.bookInfo.book_name;
                            coverImg         = itemVO.bookInfo.book_unruly;
                            tags             = itemVO.bookInfo.tags;

                            lastReadChapterId    = itemVO.lastReadChapter.id;
                            lastReadChapterName  = itemVO.lastReadChapter.chapter_name;

                            shortTitle = String.format("%s话/%s话", itemVO.lastReadChapter.chapter_px, itemVO.lastUpdateChapter.chapter_px);
                        }

                        moduleEntity.addModuleItem(bookId, bookName, title, shortTitle, coverImg, tags,
                                freeNum, floatTips, lastReadChapterId, lastReadChapterName, isTeensVisible);
                    }
                }
                if(ObjectUtil.nonNull(moduleVO.data)) {
                    ModuleAdVO adVO = moduleVO.data;
                    moduleEntity.setAdvertEntity(adVO.img, adVO.is_open == 1, /*advertType,*/ adVO.oparam, adVO.target, adVO.title);
                }
                if(ObjectUtil.nonNull(moduleVO.rank)) {
                    RankListVO rankListVO = moduleVO.rank;

                    moduleEntity.clearModuleRankList();

                    //畅销
                    HomeModuleRankEntity hotSaleRank = moduleEntity.new HomeModuleRankEntity("畅销榜");
                    for(RankInfoVO rankInfoVO : rankListVO.sell) {
                        int    bookId       = rankInfoVO.id;
                        String coverUrl     = rankInfoVO.book_unruly;
                        String bookName     = rankInfoVO.book_name;
                        String popularity   = "人气值："+ StringUtil.toSymbol(rankInfoVO.read_cnt);
                        String describe     = rankInfoVO.short_title;

                        hotSaleRank.addRankItemEntity(bookId, coverUrl, bookName, popularity, describe);
                    }
                    //新作
                    HomeModuleRankEntity newRank = moduleEntity.new HomeModuleRankEntity("新作榜");
                    for (RankInfoVO rankInfoVO : rankListVO.rank_new) {
                        int    bookId       = rankInfoVO.id;
                        String coverUrl     = rankInfoVO.book_unruly;
                        String bookName     = rankInfoVO.book_name;
                        String popularity   = "人气值："+ StringUtil.toSymbol(rankInfoVO.read_cnt);
                        String describe     = rankInfoVO.short_title;

                        newRank.addRankItemEntity(bookId, coverUrl, bookName, popularity, describe);
                    }
                    //免费
                    HomeModuleRankEntity freeRank = moduleEntity.new HomeModuleRankEntity("免费");
                    for (RankInfoVO rankInfoVO : rankListVO.free) {
                        int    bookId       = rankInfoVO.id;
                        String coverUrl     = rankInfoVO.book_unruly;
                        String bookName     = rankInfoVO.book_name;
                        String popularity   = "人气值："+ StringUtil.toSymbol(rankInfoVO.read_cnt);
                        String describe     = rankInfoVO.short_title;

                        freeRank.addRankItemEntity(bookId, coverUrl, bookName, popularity, describe);
                    }
                    if(hotSaleRank.isNotEmpty())
                        moduleEntity.addModuleRankItem(hotSaleRank);
                    if(newRank.isNotEmpty())
                        moduleEntity.addModuleRankItem(newRank);
                    if(freeRank.isNotEmpty())
                        moduleEntity.addModuleRankItem(freeRank);
                }

                viewModel.addModule(moduleEntity.buildModuleViewModels());
            }
            viewModel.refreshModuleAdapter();
        }

        if(ObjectUtil.nonNull(data.guess)) {
            HomeModuleEntity guessModuleEntity = new HomeModuleEntity(-1, "猜你喜欢", HomeModuleType.TYPE_GUESS);
            for(GuessItemVO itemVO : data.guess) {
                int bookId              = itemVO.id;
                String bookName         = itemVO.book_name;
                String title            = "";
                String coverImg         = itemVO.book_unruly;
                String[] tags           = itemVO.tags;
                String floatTips        = ""; // 空值传递将不会展示
                String shortTitle       = "";
                int freeNum             = 0;
                int lastReadChapterId   = 0;
                String lastReadChapterName = "";
                boolean  isTeensVisible = itemVO.isTeensVisible();

                guessModuleEntity.addModuleItem(bookId, bookName, title, shortTitle, coverImg, tags,
                        freeNum, floatTips, lastReadChapterId, lastReadChapterName, isTeensVisible);
            }
            viewModel.addModule(guessModuleEntity.buildModuleViewModels());
//        guessModuleEntity.refreshGuessRandom();
//        return guessModuleEntity;
        }

        if(ObjectUtil.nonNull(data.teenager)) {

        }

        viewModel.refreshUI();
    }

} 
